
//
//  TopicDetailViewController.m
//  XWZ_News_Work
//
//  Created by penngshi on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicDetailViewController.h"
#import "DetailView.h"
#import "FindDetailHostTableViewCell.h"
#import "HotListModel.h"
#import "QuestionModel.h"
#import "AnswerModel.h"
@interface TopicDetailViewController ()<UITableViewDataSource,UITableViewDelegate, TrunCellHeightDelegate,ReturnCellHeightDelegate, TrunHostCellHeightDelegate , ReturnHostCellHeightDelegate, lastMessageDelegate,hostMessageDelegate>
@property (nonatomic , strong) UIImageView *bImageV;
//创建tableView
@property (nonatomic ,strong)UITableView *myTableView;
//大图片
@property (nonatomic ,strong)UIImageView *bigImageV;
//大图片上的解析
@property (nonatomic ,strong)UILabel *aliasLabel;
//大图片上的关注数
@property (nonatomic ,strong)UILabel *countLabel;
//小网易
@property (nonatomic ,strong)UIImageView *smallWYV;
//
@property (nonatomic , strong) NSMutableDictionary *bigDic;

@property (nonatomic , strong) NSMutableArray *hotListArr;

@property (nonatomic , strong) UIImageView *titleImageV;
@property (nonatomic , strong) UILabel *titleLabel;
@property (nonatomic , assign) BOOL ret;
@property (nonatomic , assign) BOOL ret1;
@property (nonatomic , strong) NSString *urlStr;

@property (nonatomic , strong) NSMutableArray *latestListArr;

@property (nonatomic , strong) NSMutableArray *tempArr;
@property (nonatomic , strong) NSMutableArray *tempHostArr;
@property (nonatomic , assign) BOOL lastrRet;
@end

@implementation TopicDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
      self.view.backgroundColor = [UIColor lightGrayColor];
    
    
    
  
    // Do any additional setup after loading the view.
    [self requestData];
#pragma mark - imageView 不随滑动而改变
    //    [self setAutomaticallyAdjustsScrollViewInsets:NO];
#pragma mark - 创建大图片上的视图

    [self createSubView];
    [self createtitle];
   
    
#pragma mark - 右边的button的分享功能
    [self createRightButton];
#pragma mark - 左边的button返回功能
    [self createLeftButton];
#pragma mark - 注册cell
//    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
}
- (void)requestData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", self.expertId];
    NSLog(@"~~~~~~~%@" , self.expertId);
    self.urlStr = urlStr;
    [xwzAFNetTool getNetWithURL:urlStr body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dicData = [dic objectForKey:@"data"];
        self.bigDic = [NSMutableDictionary dictionary];
        self.bigDic = [dicData objectForKey:@"expert"];
        self.hotListArr = [NSMutableArray array];
       
       
        
        
       
        NSMutableArray *arr = [dicData objectForKey:@"hotList"];
        for (NSMutableDictionary *dicArr in arr) {
            HotListModel *allM = [[HotListModel alloc] init];
            QuestionModel *qm = [[QuestionModel alloc] initWithDic:[dicArr objectForKey:@"question"]];
            AnswerModel *am = [[AnswerModel alloc] initWithDic:[dicArr objectForKey:@"answer"]];
            allM.questModel = qm;
            allM.answerModel = am;
            [self.hotListArr addObject:allM];
            
            
        }
        
        
         self.latestListArr = [NSMutableArray array];
        NSMutableArray *latestListArr = [dicData objectForKey:@"latestList"];
        for (NSMutableDictionary *dicArr in latestListArr) {
            HotListModel *allM = [[HotListModel alloc] init];
            QuestionModel *qm = [[QuestionModel alloc] initWithDic:[dicArr objectForKey:@"question"]];
            AnswerModel *am = [[AnswerModel alloc] initWithDic:[dicArr objectForKey:@"answer"]];
            allM.questModel = qm;
            allM.answerModel = am;
            [self.latestListArr addObject:allM];
            
            
        }

        [self valueHeader];
        [self .myTableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)valueHeader
{
 
    [self.bigImageV sd_setImageWithURLStr:[self.bigDic objectForKey:@"picurl"] placeholderImage:[UIImage imageNamed:@"lol-tupian"] ];
    self.aliasLabel.text = [self.bigDic objectForKey:@"alias"];
    self.aliasLabel.textColor = [UIColor whiteColor];
    self.countLabel.text = [NSString stringWithFormat:@"一%@关注一",[self.bigDic objectForKey:@"concernCount"]];
}
- (void)createSubView {
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 50) style:UITableViewStyleGrouped];
    self.myTableView.contentInset = UIEdgeInsetsMake(250, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[FindeDeatilTableViewCell class] forCellReuseIdentifier:@"findeDeatilcell"];
    [self.myTableView registerClass:[FindDetailHostTableViewCell class] forCellReuseIdentifier:@"findeHostDeatilcell"];
    
    [self.view addSubview:self.myTableView];
    
#pragma mark - 创建大图片
    self.bigImageV = [[UIImageView alloc] init];
    //    self.bigImageV = [[UIImageView alloc] init];
    self.bigImageV.frame = CGRectMake(0, -250, 375, 250);
    self.bigImageV.backgroundColor = [UIColor lightGrayColor];
  
    self.bigImageV.contentMode = UIViewContentModeScaleAspectFill;
    [self.myTableView addSubview:self.bigImageV];
    
    
    self.bImageV = [[UIImageView alloc] init];
    //    self.bigImageV = [[UIImageView alloc] init];
    self.bImageV.frame = CGRectMake(0, -250, 375, 250);
    self.bImageV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
   self.bImageV.userInteractionEnabled = YES;
    self.bImageV.contentMode = UIViewContentModeScaleAspectFill;
    [self.myTableView addSubview:self.bImageV];
    
#pragma mark - 创建大图片上的label
    self.aliasLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 315, 60)];
    self.aliasLabel.numberOfLines = 3;
    self.aliasLabel.textAlignment = NSTextAlignmentCenter;
    //    self.aliasLabel.backgroundColor = [UIColor lightGrayColor];
    self.aliasLabel.textColor = [UIColor whiteColor];
    [self.bImageV addSubview:self.aliasLabel];
    
#pragma mark - 大图片上的关注数
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 125, 235, 30)];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    self.countLabel.textColor = [UIColor grayColor];
    
    [self.bigImageV addSubview:self.countLabel];
    
#pragma mark - 网易小图片
    self.smallWYV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar_netease@2x.png"]];
    self.smallWYV.frame = CGRectMake(157, 190, 60, 30);
    [self.bigImageV addSubview:self.smallWYV];
    
    
    
    
}

#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (  0 == indexPath.section) {
        FindeDeatilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"findeDeatilcell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.headerView sd_setImageWithURLStr:[self.bigDic objectForKey:@"headpicurl"] placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        NSString *str = [NSString stringWithFormat:@"%@|%@", [self.bigDic objectForKey:@"name"] ,[self.bigDic objectForKey:@"title"]];
        cell.nameLabel.text = str  ;
//        cell.nameValueLabel.text =   ;
        cell.cententLabel.text = [self.bigDic objectForKey:@"description"];
        cell.layer.cornerRadius = 15;
        cell.layer.shadowColor = [[UIColor grayColor] CGColor];
        cell.layer.borderWidth = 2;
        cell.layer.borderColor = [[UIColor grayColor] CGColor];
        //阴影偏移量
        cell.layer.shadowOffset = CGSizeMake(2, 2);
        //透明度
        cell.layer.shadowOpacity = 0.5;
        //模糊度
        cell.layer.shadowRadius = 0.5;
        cell.delegate = self;
        cell.delegate1 = self;
        return cell;
    }else
    {
        self.tempHostArr = [NSMutableArray array];
        self.tempArr  = [NSMutableArray array];
         self.tempArr = self.hotListArr;
        if (self.lastrRet == YES) {
//            self.lastrRet = NO;
          
            self.tempArr  = self.latestListArr;
        } else
        {
           
             self.tempArr = self.hotListArr ;
        }
        FindDetailHostTableViewCell *hostCell = [tableView dequeueReusableCellWithIdentifier:@"findeHostDeatilcell" forIndexPath:indexPath];
        hostCell.selectionStyle = UITableViewCellSelectionStyleNone;
       
        [hostCell.userHeaderView sd_setImageWithURLStr: [[[self.tempArr objectAtIndex: indexPath.row] questModel]userHeadPicUrl] placeholderImage:[UIImage imageNamed:@"touxiang"]];
        hostCell.userNameLabel.text  = [[[self.tempArr objectAtIndex: indexPath.row] questModel]userName];
        hostCell.userCententLabel.text = [[[self.tempArr objectAtIndex: indexPath.row] questModel]content];
        [hostCell.answerHeaderView sd_setImageWithURLStr:[[[self.tempArr objectAtIndex: indexPath.row] answerModel]specialistHeadPicUrl] placeholderImage:[UIImage imageNamed:@"touxiang"]];
        hostCell.answerNameLabel.text  = [[[self.tempArr objectAtIndex: indexPath.row] answerModel]specialistName];
        hostCell.answerCententLabel.text = [[[self.tempArr objectAtIndex: indexPath.row] answerModel]content];
        NSString *requeatStr = [NSString stringWithFormat:@"%@",[[[self.tempArr objectAtIndex: indexPath.row] answerModel]replyCount]];
        hostCell.replyCountLabel.text = requeatStr;
        hostCell.replyCountImageV.image = [UIImage imageNamed:@"pinglun"];
        hostCell.supportCountImageV.image = [UIImage imageNamed:@"dianzan32x32"];
        
        NSString *supportStr = [NSString stringWithFormat:@"%@",[[[self.tempArr objectAtIndex: indexPath.row] answerModel]supportCount]];
        hostCell.supportCountLabel.text = supportStr;
        if ([[[self.tempArr objectAtIndex: indexPath.row] answerModel]cTime] != nil) {
            
             NSString *str = [NSString stringWithFormat:@"%@" , [[[self.tempArr objectAtIndex: indexPath.row] answerModel]cTime]];
             NSString *str2 = [str substringToIndex:10];
            double aNumber = [str2 doubleValue];
            NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:aNumber];
            NSString *astr = [NSString stringWithFormat:@"%@",confromTimesp];
            NSString *date = [astr substringToIndex:10];
            hostCell.timeLabel.text = date;
            hostCell.timeLabel.textColor = [UIColor grayColor];
           
        } else
        {
            nil;
        }
        hostCell.delegate = self;
        hostCell.delegate1 = self;
        hostCell.layer.cornerRadius = 15;
           hostCell.layer.shadowColor = [[UIColor grayColor] CGColor];
        hostCell.layer.borderWidth = 2;
        hostCell.layer.borderColor = [[UIColor grayColor] CGColor];
           
        return hostCell;
    }

 
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        
        if (self.ret == YES) {
         
                    CGRect frame = [[self.bigDic objectForKey:@"description"] boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName]context:nil];

                    return frame.size.height + 120;
        } else{
        

             return 150;
        }
     

    } else
    {
        self.tempHostArr = [NSMutableArray array];
        self.tempArr  = [NSMutableArray array];
        self.tempArr = self.hotListArr;
        if (self.lastrRet == YES) {
            //            self.lastrRet = NO;
            
            self.tempArr  = self.latestListArr;
        } else
        {
            
            self.tempArr = self.hotListArr ;
        }
        NSString *str = [[[self.tempArr objectAtIndex: indexPath.row] questModel]content];
         NSString *str1 = [[[self.tempArr objectAtIndex: indexPath.row] answerModel]content];
         CGRect frame = [str boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName]context:nil];
        CGRect frame1 = [str1 boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName]context:nil];
        return frame.size.height + 250 + frame1.size.height;
    }
    
    
}
- (void)TrunCellHeight
{
    self.ret = YES;
    [self.myTableView reloadData];
}
- (void)ReturnCellHeight
{
    self.ret = NO;
    [self.myTableView reloadData];
    
}
- (void)TrunHostCellHeight
{
    self.ret1 = YES;
    [self.myTableView reloadData];
}
//- (void)ReturnHostCellHeight
//{
//    self.ret1 = NO;
//    [self.myTableView re]
//}


- (void)lastMessage
{
   
    self.lastrRet = YES;
      [self.myTableView reloadData];
}
- (void)hostMessage
{

    self.lastrRet = NO;
    
    [self.myTableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (  0 == section) {
        return 0.1;
    } else
    {
        return 30;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (0 !=section) {
        DetailView *detailV  = [[DetailView alloc]initWithFrame:CGRectMake(0, -10, SCREEN_WIDTH, 30)];
        detailV.delegate = self;
        detailV.delegatehost = self;
        detailV.userInteractionEnabled = YES;
        detailV.requeatLabel.text =  [NSString stringWithFormat:@"%@提问",[self.bigDic objectForKey:@"questionCount"]] ;
        detailV.requeatLabel.textColor = [UIColor grayColor];
          detailV.answerLabel.text =[NSString stringWithFormat:@"%@回答",[self.bigDic objectForKey:@"answerCount"]];
        detailV.answerLabel.textColor = [UIColor grayColor];
         ;
        return detailV;
    } else
    {
        return nil;
    }
   
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
#pragma mark - 两个必要的方法
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (0 == section) {
        return 1;
    } else
    {
        return self.hotListArr.count;
    }
}

#pragma mark - 左上角的button
- (void)createLeftButton
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(22 * SIZE, 18 * SIZE, 42 * SIZE, 42 * SIZE);
    [leftButton setImage:[UIImage imageNamed:@"huati-fanhui (1)"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:leftButton];

      [self.titleImageV addSubview:leftButton];
}

#pragma mark - 右上角的button
- (void)createRightButton
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake((367 - 54) * SIZE, 18 * SIZE, 42 * SIZE, 42 * SIZE);
    [rightButton setImage:[UIImage imageNamed:@"fenxiang-1"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:rightButton];
    [self.titleImageV addSubview:rightButton];

}
- (void)createtitle
{
    self.titleImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
    self.titleImageV.userInteractionEnabled = YES;
    [self.view addSubview:self.titleImageV];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 18 * SIZE, SCREEN_WIDTH - 110, 40)];
    self.titleLabel.text = self.alias;
    [self.titleImageV addSubview:self.titleLabel];
}
#pragma mark - 效果的方法的实现
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    self.titleImageV.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:scrollView.contentOffset.y /  + 318];
    self.titleLabel.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:scrollView.contentOffset.y /  + 318];
//     self.titleImageV.backgroundColor = scrollView.contentOffset.y /  + 318;
    //刚开始y的偏移量初始值就是-264
//    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
//    NSLog(@"y2 === %f",y);
    
    if (y < -250) {
        CGRect frame = self.bigImageV.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.bigImageV.frame = frame;
        NSLog(@"%f", y);
        
        CGRect frame1 = self.bImageV.frame;
        frame1.origin.y = y;//偏移了多少,
        frame1.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.bImageV.frame = frame1;
    }
    
}


#pragma mark - 左侧按钮方法
- (void)leftButtonAction:(UIButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 右侧按钮方法
- (void)rightButtonAction:(UIButton *)btn
{
    NSString *str = [NSString stringWithFormat:@"%@|%@", self.alias,self.urlStr];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:str
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,nil]
                                       delegate:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
